package com.banach.prema.executor;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.banach.prema.constant.MsgConstants;
import com.banach.prema.constant.MsgType;
import com.banach.prema.dto.cmd.SosCmd;
import com.banach.prema.enums.SosStatus;
import com.banach.prema.enums.SosType;
import com.banach.prema.im.rest.GroupRestApi;
import com.banach.prema.repository.SosRepository;
import com.banach.prema.repository.dataobject.SosDO;
import com.ego.base.api.LocationRemoteService;
import com.ego.base.dto.vo.AddressVO;
import com.ego.framework.core.context.UserHolder;
import com.ego.framework.core.tookit.BeanToolkit;
import com.ego.framework.core.util.ClassUtils;
import com.ego.framework.core.util.StringUtils;
import com.ego.framework.dto.Response;
import com.ego.framework.model.LoginUser;
import com.ego.msg.provider.SmsProvider;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author ajun
 * @Desc
 * @Date 2025/3/24 11:33
 */
@Component
public class SosSendExe {

    @Resource
    private SosRepository sosRepository;
    @Resource
    private LocationRemoteService locationRemoteService;

    @Resource
    private GroupRestApi groupRestApi;
    @Resource
    private SmsProvider smsProvider;

    public Response send(SosCmd sosCmd) {
        AddressVO address = locationRemoteService.getAddress(sosCmd.getLng(), sosCmd.getLat()).getData();
        LoginUser user = UserHolder.getLoginUser();
        final SosDO sos = BeanToolkit.getInstance().copy(sosCmd, SosDO.class);
        ClassUtils.assign(sos, address);
        sos.setUserId(user.getId());
        sos.setUserName(user.getName());
        sos.setCreateTime(LocalDateTime.now());
        sos.setUpdateTime(LocalDateTime.now());
        sos.setMobile(user.getMobile());
        sos.setStatus(SosStatus.IN_PROGRESS.getValue());
        SosType sosType = SosType.getSosType(sosCmd.getType());
        final String groupId = groupRestApi.createSosGroup(user);
        sos.setGroupId(groupId);
        sosRepository.create(sos);
        Map params = new HashMap();
        params.put("userName", user.getName());
        params.put("mobile", user.getMobile());
        params.put("address", address.getFullAddr());
        params.put("sosType", sosType.getDesc());
        smsProvider.send(MsgConstants.SMS_KEY_SOS, user.getMobile(), params);
        sendImMsg(address,sosType.getDesc(),sosCmd.getContent(), groupId);
        return Response.success("发送成功");
    }

    /**
     * 发送消息
     * @param address
     * @param groupId
     */
    private void sendImMsg(AddressVO address,String type,String content,String groupId) {
        LoginUser appUser = UserHolder.getLoginUser();
        String title = appUser.getName()+"在"+address.getSimpleAddr()+"附近遇到危险，发起"+type+"！";
        String msgText = "我在"+address.getFullAddr()+"附近遇到危险，发起"+type+"！";
        if(StringUtils.isNotEmpty(content)){
            msgText += "\n"+content;
        }
        JSONArray msgBody = new JSONArray();
        JSONObject textMsg = new JSONObject();
        textMsg.put("MsgType", MsgType.TEXT);
        JSONObject msgContent = new JSONObject();
        msgContent.put("Text", msgText);
        textMsg.put("MsgContent", msgContent);
        msgBody.add(textMsg);
        JSONObject locationMsg = new JSONObject();
        locationMsg.put("MsgType", MsgType.LOCATION);
        JSONObject msgContent2 = new JSONObject();

        msgContent2.put("Desc", address.getSimpleAddr());
        msgContent2.put("Latitude", address.getLat());
        msgContent2.put("Longitude", address.getLng());
        locationMsg.put("MsgContent", msgContent2);
        msgBody.add(locationMsg);

        JSONObject offlinePushInfo = new JSONObject();
        offlinePushInfo.put("PushFlag", 0);
        offlinePushInfo.put("Desc", title);
        offlinePushInfo.put("Ext", title);

        JSONObject androidInfo = new JSONObject();
        androidInfo.put("Sound", "android.mp3");
        JSONObject apnsInfo = new JSONObject();
        apnsInfo.put("Sound", "apns.mp3");
        apnsInfo.put("BadgeMode", 0);
        apnsInfo.put("Title", title);
        offlinePushInfo.put("AndroidInfo", androidInfo);
        offlinePushInfo.put("ApnsInfo", apnsInfo);
        groupRestApi.sendMsg(appUser.getMobile(), groupId, msgBody,offlinePushInfo);
    }
}
